#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "tcm_view.h"

// add para lib_include
#include "transfer_tcm.h"


int proc_dispatch(void * sub_proc,void * message);

int transfer_tcm_init(void * sub_proc,void * para)
{
	int ret;
	// add youself's plugin init func here
	return 0;
}

int transfer_tcm_start(void * sub_proc,void * para)
{
	int ret;
	void * recv_msg;
	int i;
	int type;
	int subtype;


	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
				message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(TCM_OBJECT_QUERY))&&(subtype==SUBTYPE(TCM_OBJECT_QUERY,NODE)))
		{
			ret=proc_node_addtarget(sub_proc,recv_msg);
		}
		else if((type==TYPE(TCM_OBJECT_QUERY))&&(subtype==SUBTYPE(TCM_OBJECT_QUERY,PRIVATEKEY)))
		{
			ret=proc_privatekey_addtarget(sub_proc,recv_msg);
		}
		else if((type==TYPE(TCM_OBJECT_QUERY))&&(subtype==SUBTYPE(TCM_OBJECT_QUERY,PUBLICKEY)))
		{
			ret=proc_publickey_addtarget(sub_proc,recv_msg);
		}
	}
	return 0;
};

int proc_node_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(TCM_OBJECT_VIEW,NODE) * node_state;
	RECORD(TCM_OBJECT_QUERY,NODE) * node_query;
	
	DB_RECORD * db_record;

        ret=message_get_record(recv_msg,&node_query,0);
        if(ret<0)
                return ret;
	
        if(node_query==NULL)
                return -EINVAL;

	target=Talloc0(sizeof(*target));
	db_record=memdb_find_first(TYPE_PAIR(TCM_OBJECT_VIEW,NODE),"uuid",node_query->uuid);
        if(db_record==NULL)
        {
                printf("tcm node %s can't be find!\n",node_query->name);
                return -EINVAL;
        }

	node_state=db_record->record;

	comp_proc_uuid(node_state->uuid,node_state->proc_name,target->uuid);

        ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
        if(ret<0)
                return ret;
        ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}

int proc_privatekey_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(TCM_OBJECT_VIEW,NODE) * node_state;
	RECORD(TCM_OBJECT_QUERY,PRIVATEKEY) * key_query;
	
	DB_RECORD * db_record;

        ret=message_get_record(recv_msg,&key_query,0);
        if(ret<0)
                return ret;
	
        if(key_query==NULL)
                return -EINVAL;

	target=Talloc0(sizeof(*target));
	db_record=memdb_find_first(TYPE_PAIR(TCM_OBJECT_VIEW,NODE),"uuid",key_query->tcm_uuid);
        if(db_record==NULL)
        {
                printf("tcm node %8s can't be find!\n",key_query->tcm_uuid);
                return -EINVAL;
        }

	node_state=db_record->record;

	comp_proc_uuid(node_state->uuid,node_state->proc_name,target->uuid);

        ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
        if(ret<0)
                return ret;
        ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}
int proc_publickey_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(TCM_OBJECT_VIEW,NODE) * node_state;
	RECORD(TCM_OBJECT_QUERY,PUBLICKEY) * key_query;
	
	DB_RECORD * db_record;

        ret=message_get_record(recv_msg,&key_query,0);
        if(ret<0)
                return ret;
	
        if(key_query==NULL)
                return -EINVAL;

	target=Talloc0(sizeof(*target));
	db_record=memdb_find_first(TYPE_PAIR(TCM_OBJECT_VIEW,NODE),"uuid",key_query->node_uuid);
        if(db_record==NULL)
        {
                printf("tcm node %8s can't be find!\n",key_query->node_uuid);
                return -EINVAL;
        }

	node_state=db_record->record;

	comp_proc_uuid(node_state->uuid,node_state->proc_name,target->uuid);

        ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
        if(ret<0)
                return ret;
        ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}
